﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using MSSQLProvider.DBConnection;


namespace MSSQLProvider
{
    public partial class DBConnecter : Form
    {
        public DBConnecter()
        {
            InitializeComponent();
        }


        //MYSQL: Database=nutritioncarepad;Data Source=localhost; User Id=root;password=fish;charset=utf8;
        //SQL Server:User Id=sa;password=fish;Data Source=dbs\new_manual;Initial Catalog=his08;Integrated Security=false
        private SQLServerConnection _connectionInfo;
        public SQLServerConnection ConnectionInfo
        {
            get
            {
                var str = string.Format("User Id={2};password={3};Data Source={0};Initial Catalog={1};Integrated Security=false;", txtServer.Text, txtDB.Text, txtUID.Text, txtPwd.Text);
                if (_connectionInfo == null)
                {
                    _connectionInfo = new SQLServerConnection(txtName.Text, str);
                }
                else
                {
                    _connectionInfo.SetName(txtName.Text);
                    _connectionInfo.SetConnectStr(str);
                }
                return _connectionInfo;
            }
            set
            {
                _connectionInfo = value;
                txtName.Text = _connectionInfo.Name;
                txtConnectStr.Text = _connectionInfo.ConnectStr;

                var dic = getConDic(_connectionInfo.ConnectStr);
                if (dic.Count > 0)
                {
                    txtServer.Text = dic["Data Source"];
                    txtDB.Text = dic["Initial Catalog"];
                    txtUID.Text = dic["User Id"];
                    txtPwd.Text = dic["password"];
                }
            }
        }


        private Dictionary<string, string> getConDic(string conStr)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            var cons = conStr.Split(new char[] { ';' });
            foreach (var cds in cons)
            {
                var cd = cds.Split(new char[] { '=' });
                if (cd.Length != 2)
                {
                    continue;
                }
                dic.Add(cd[0].Trim(), cd[1].Trim());
            }
            return dic;
        }


        private void btOK_Click(object sender, EventArgs e)
        {
            txtName.BackColor = Color.White;
            txtConnectStr.BackColor = Color.White;

            if (txtName.Text.Trim().Length == 0)
            {
                txtName.BackColor = Color.LightYellow;
                return;
            }

            if (txtConnectStr.Text.Trim().Length == 0)
            {
                txtConnectStr.BackColor = Color.LightYellow;
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            txtConnectStr.Text = string.Format("User Id={2};password={3};Data Source={0};Initial Catalog={1};Integrated Security=false;", txtServer.Text, txtDB.Text, txtUID.Text, "****");

        }


    }

}
